package markgame2dphys;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import java.util.Random;

import javax.swing.JFrame;

import net.phys2d.math.Vector2f;
import net.phys2d.raw.Body;

public class Test4_Bike extends MarkGame2DEngine{

	private static final long serialVersionUID = 1L;

	public static final int W = 1024;
	public static final int H = 600;

	
	public MarkGameCircle roda1;
	public MarkGameCircle roda2;
	public MarkGameBox quadro;
	public MarkGamePolygon base;

	public MarkGameBasicJoint joint1;
	public MarkGameBasicJoint joint2;
	
	public Test4_Bike() {
		super(W,H,0,100, 20, 30);

		quadro = new MarkGameBox(100, 50, 2);
		quadro.body.setPosition(100, H-200);
		quadro.body.setRestitution(0.5f);
		add(quadro);
		
		roda1 = new MarkGameCircle(25, 0.5f);
		roda1.body.setPosition(quadro.body.getPosition().x-quadro.width/2, quadro.body.getPosition().y+quadro.height/2);
		roda1.body.setRestitution(0.7f);
		//roda1.body.setFriction(0.2f);//default
		roda1.body.setFriction(1f);
		add(roda1);
		
		roda2 = new MarkGameCircle(25, 0.5f);
		roda2.body.setPosition(quadro.body.getPosition().x+quadro.width/2, quadro.body.getPosition().y+quadro.height/2);
		roda2.body.setRestitution(0.7f);
		roda2.body.setFriction(1f);
		add(roda2);
		
		joint1 = new MarkGameBasicJoint(roda1, quadro, roda1.body.getPosition());
		joint2 = new MarkGameBasicJoint(roda2, quadro, roda2.body.getPosition());
		add(joint1);
		add(joint2);
		
		quadro.body.addExcludedBody(roda1.body);
		quadro.body.addExcludedBody(roda2.body);
		
		Random random = new Random();
		float dif = 20;
		int steps = 30;
		
		LinkedList<Vector2f> points = new LinkedList<Vector2f>();
		points.add(new Vector2f(-W/2, -15-300));
		for (int i = -(steps-1); i <= (steps-1); i++)
			points.add(new Vector2f(W/2*(1f/steps)*i, -15+(random.nextFloat()*dif-dif/2)));
		points.add(new Vector2f(+W/2, -15-300));
		points.add(new Vector2f(+W/2, +5));
		points.add(new Vector2f(-W/2, +5));
		
		base = new MarkGamePolygon(points.toArray(new Vector2f[]{}), Body.INFINITE_MASS);
		base.body.setPosition(W/2, H-10);
		base.body.setRestitution(0.5f);
		base.body.setFriction(1f);
		add(base);
		
	}
	
	@Override
	public void mousePressed(MouseEvent e) {
		if(e.getX() < W/2){//left
			//roda2.body.setTorque(-5000);
			roda1.body.adjustAngularVelocity(-10);
			roda2.body.adjustAngularVelocity(-10);
		}else{//right
			//roda2.body.setTorque(+5000);
			roda1.body.adjustAngularVelocity(+10);
			roda2.body.adjustAngularVelocity(+10);
		}
	}

	public void paint(MarkGameGraphics g) {
		g.setColor(Color.WHITE);
		g.fillRect(0,0,W,H);

		paintAllObjects(g);
		paintAllJoints(g);
	}

	public static void main(String[] args) {

		JFrame janela = new JFrame("teste1");
		janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		Test4_Bike app = new Test4_Bike();

		janela.getContentPane().add(app);

		janela.pack();
		janela.setVisible(true);

		janela.setLocationRelativeTo(null);

		janela.setExtendedState(JFrame.MAXIMIZED_BOTH);

		new Thread(app).start();

	}

}
